package com.kbbywyclient.presenter;

import android.content.Context;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.ResultInfoCre;
import com.kbbywyclient.data.model.CreditCardBuyModel;
import com.kbbywyclient.data.model.CreditCardTableMenuModel;
import com.kbbywyclient.data.model.CustomerTableModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.CustomerTableContarct;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.customservice.ApiException;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Description:
 * Created by FHW
 * on 2017/4/17
 */

public class CustomerTablePresenter extends BasePresenter implements CustomerTableContarct.Presenter {

    private CustomerTableContarct.View view;
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil rsaJsonUtil;

    private int pageno = 1;
    private List<ResultInfoCre> resultInfoCreList = null;

    @Inject
    public CustomerTablePresenter(CustomerTableContarct.View view) {
        this.view = view;
        resultInfoCreList = new ArrayList<>();
        this.view.setAdapterOrNotify(resultInfoCreList);
    }

    @Override
    public void getCreditCardOrders(boolean isClean, String SalesID, String Dist, String Education, String UnitNature, String CreditCard) {
        if (isClean) {
            pageno = 1;
        }
        rsaJsonUtil.put("pageno", pageno + "");
        rsaJsonUtil.put("pageSize", API.PAGE_SIZE);

        rsaJsonUtil.put("SalesID", SalesID);
        if (StringUtil.isNotEmpty(Dist)) {
            rsaJsonUtil.put("Dist", Dist);
        }
        if (!Education.equals("0")) {
            rsaJsonUtil.put("Education", Education);
        }
        if (!UnitNature.equals("0")) {
            rsaJsonUtil.put("UnitNature", UnitNature);
        }
        if (StringUtil.isNotEmpty(CreditCard)) {
            rsaJsonUtil.put("CreditCard", CreditCard);
        }

        ApiServiceHelper.
                appointThreadNormal(apiService.getCreditCardOrders(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<CustomerTableModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.loadFaiCustomerTable(e.toString());
                    }

                    @Override
                    public void onNext(CustomerTableModel customerTableModel) {
                        if (isClean) {
                            resultInfoCreList.clear();
                        }
                        resultInfoCreList.addAll(customerTableModel.getData().getResult());
                        view.setAdapterOrNotify(resultInfoCreList);//刷新
                        view.loadSucCustomerTable(resultInfoCreList);
                        //分页
                        if (customerTableModel.getData().getPaging().getCurrentPage() >= customerTableModel.getData().getPaging().getTotalPage()) {
                            if (customerTableModel.getData().getResult().size() > 1) {
                                view.setNoMore();
                            }
                        } else {
                            pageno++;
                            view.setMoreComplete();
                        }
                    }
                });

    }

    @Override
    public void getCreditCardMenu(String SalesID) {
        rsaJsonUtil.put("SalesID", SalesID);
        ApiServiceHelper.
                appointThreadNormal(apiService.getCreditCardMenu(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<CreditCardTableMenuModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(CreditCardTableMenuModel creditCardTableMenuModel) {
                        view.loadSucMenu(creditCardTableMenuModel.getData());
                    }
                });
    }

    @Override
    public void getApplyCreditCardBuy(String SalesID, String ApplyCreditCardID, String ApplyCreditCardSN, String BuyType) {
        rsaJsonUtil.put("SalesID", SalesID);
        rsaJsonUtil.put("ApplyCreditCardID", ApplyCreditCardID);
        rsaJsonUtil.put("ApplyCreditCardSN", ApplyCreditCardSN);
        if (StringUtil.isNotEmpty(BuyType)) {
            view.showLoadingDialog(mContext.getString(R.string.text_loading));
            rsaJsonUtil.put("BuyType", BuyType);
        }
        ApiServiceHelper.
                appointThreadNormal(apiService.getApplyCreditCardBuy(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<CreditCardBuyModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        if (e instanceof ApiException) {
                            view.loadFaiCreditCarBuy(e.getMessage());
                        } else {
                            HttpExcUtil.printError(e);
                        }

                    }

                    @Override
                    public void onNext(CreditCardBuyModel creditCardBuyModel) {
                        view.loadSucCreditCarBuy(creditCardBuyModel.getData(), BuyType);
                    }
                });


    }


}
